# -*- tcl -*-
# # ## ### ##### ######## ############# #####################
##
# Test suite for Tcl/Hwloc binding.
# Bitmap manipulation commands.

# # ## ### ##### ######## ############# #####################
## Requirements

package require Tcl 8.5

if {[catch {package require tcltest 2}]} {
    puts stderr "Skipping tests in [info script].  tcltest 2 required."
    return
}

# # ## ### ##### ######## ############# #####################

test hwloc-bitmap-1.0 {wrong\#args, missing method} -body {
    hwloc bitmap
} -returnCodes error \
    -result {wrong # args: should be "hwloc bitmap option ?arg? ..."}

test hwloc-bitmap-1.1 {bad method name} -body {
    hwloc bitmap xxx
} -returnCodes error \
    -result {bad option "xxx": must be allbut, and, andnot, clear, clear_range, compare, compare_first, empty, first, from_ulong, full, intersects, is_empty, is_equal, is_full, is_included, is_set, last, next, not, only, or, set, set_range, singlify, to_ulong, weight, or xor}

# # ## ### ##### ######## ############# #####################
## 0-ary operators, constant bitmaps

foreach {n op} {
    0 empty
    1 full
} {
    test hwloc-bitmap-2.$n "constant bitmaps, $op, wrong args" -body {
	hwloc bitmap $op x
    } -returnCodes error \
	-result "wrong # args: should be \"hwloc bitmap $op\""
}

# - -- --- ----- -------- ------------- ---------------------

test hwloc-bitmap-2.2 {constant bitmaps, empty} -body {
    hwloc bitmap empty
} -result {}

test hwloc-bitmap-2.3 {constant bitmaps, full} -body {
    hwloc bitmap full
} -result {0-}

# # ## ### ##### ######## ############# #####################
## unary operators

foreach {n op p} {
    0  from_ulong mask
    1  allbut     id
    2  only       id
    3  first      bitmap
    4  is_full    bitmap
    5  is_empty   bitmap
    6  last       bitmap
    7  not        bitmap
    8  singlify   bitmap
    9  to_ulong   bitmap
    10 weight     bitmap
} {
    test hwloc-bitmap-3.$n.0 "unary bitmap operations, $op, wrong args" -body {
	hwloc bitmap $op
    } -returnCodes error \
	-result "wrong # args: should be \"hwloc bitmap $op $p\""

    test hwloc-bitmap-3.$n.1 "unary bitmap operations, $op, wrong args" -body {
	hwloc bitmap $op X Y
    } -returnCodes error \
	-result "wrong # args: should be \"hwloc bitmap $op $p\""

    if {$n >= 3} {
	test hwloc-bitmap-3.$n.2 "unary bitmap operations, $op, bad bitmap" -body {
	    hwloc bitmap $op X
	} -returnCodes error \
	    -result {Expected bitmap but got "X"}
    } else {
	test hwloc-bitmap-3.$n.2 "unary bitmap operations, $op, bad integer" -body {
	    hwloc bitmap $op X
	} -returnCodes error \
	    -result {expected integer but got "X"}
    }
}

# - -- --- ----- -------- ------------- ---------------------
# from_ulong, to_ulong (complementary operations)

foreach {n v r} {
    3   0 {}
    4   1 0
    5   2 1
    6   3 0-1
    7   5 0,2
    8   7 0-2
    9  11 0-1,3
    10 13 0,2-3
    11 17 0,4
    12 19 0-1,4
} {
    test hwloc-bitmap-3.0.$n "unary bitmap operations, from_ulong $v" -body {
	hwloc bitmap from_ulong $v
    } -result $r

    test hwloc-bitmap-3.9.$n "unary bitmap operations, to_ulong $r" -body {
	hwloc bitmap to_ulong $r
    } -result $v
}

# The binding treats negative numbers as unsigned, i.e. large
# positive.  This also exposes word size differences between 32/64 bit
# platforms, hence the use of tcl_platform to compute the expected
# most significant bit.

test hwloc-bitmap-3.0.13 "unary bitmap operations, from_ulong -1" -body {
    hwloc bitmap from_ulong -1
} -result 0-[expr {(8*$tcl_platform(wordSize))-1}]

# - -- --- ----- -------- ------------- ---------------------
# allbut = full & !bitmap-of(id)

foreach {n id r} {
    3 0 1-
    4 1 0,2-
    5 2 0-1,3-
    6 4 0-3,5-
    7 7 0-6,8-
} {
    test hwloc-bitmap-3.1.$n "unary bitmap operations, allbut, \"$id\"" -body {
	hwloc bitmap allbut $id
    } -result $r
}

test hwloc-bitmap-3.1.8 "unary bitmap operations, allbut, \"-1\"" -body {
    hwloc bitmap allbut -1
} -returnCodes error -result {Expected integer >= 0 but got "-1"}

# - -- --- ----- -------- ------------- ---------------------
# only = empty | bitmap-of(id)

foreach {n id} {
    3 0
    4 1
    5 2
    6 4
    7 7
} {
    test hwloc-bitmap-3.2.$n "unary bitmap operations, only, \"$id\"" -body {
	hwloc bitmap only $id
    } -result $id
}

test hwloc-bitmap-3.2.8 "unary bitmap operations, allbut, \"-1\"" -body {
    hwloc bitmap only -1
} -returnCodes error -result {Expected integer >= 0 but got "-1"}

# - -- --- ----- -------- ------------- ---------------------
# first (bit of the set, errors on empty set).
# singlify - alias of first, in essence, except it doesn't error on
# the empty set.

test hwloc-bitmap-3.3.3 "unary bitmap operations, first (empty)" -body {
    hwloc bitmap first {}
} -returnCodes error -result {first is undefined for empty bitmap}

test hwloc-bitmap-3.8.3 "unary bitmap operations, singlify (empty)" -body {
    hwloc bitmap singlify {}
} -result {}

foreach {n v r} {
    4  0     0
    5  1     1
    6  0-1   0
    7  0,2   0
    8  0-2   0
    9  0-1,3 0
    10 0,2-3 0
    11 0,4   0
    12 0-1,4 0
    13 0-    0
    14 4-    4
} {
    test hwloc-bitmap-3.3.$n "unary bitmap operations, first \"$v\"" -body {
	hwloc bitmap first $v
    } -result $r

    test hwloc-bitmap-3.8.$n "unary bitmap operations, singlify \"$v\"" -body {
	hwloc bitmap singlify $v
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# is_full

foreach {n v r} {
    3  {}    0
    4  0     0
    5  1     0
    6  0-1   0
    7  0,2   0
    8  0-2   0
    9  0-1,3 0
    10 0,2-3 0
    11 0,4   0
    12 0-1,4 0
    13 0-    1
    14 4-    0
} {
    test hwloc-bitmap-3.4.$n "unary bitmap operations, is_full \"$v\"" -body {
	hwloc bitmap is_full $v
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# is_empty

foreach {n v r} {
    3  {}    1
    4  0     0
    5  1     0
    6  0-1   0
    7  0,2   0
    8  0-2   0
    9  0-1,3 0
    10 0,2-3 0
    11 0,4   0
    12 0-1,4 0
    13 0-    0
    14 4-    0
} {
    test hwloc-bitmap-3.5.$n "unary bitmap operations, is_empty \"$v\"" -body {
	hwloc bitmap is_empty $v
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# last (last bit set, errors on empty and infinite sets)

test hwloc-bitmap-3.6.3 "unary bitmap operations, last (empty)" -body {
    hwloc bitmap last {}
} -returnCodes error -result {last is undefined for empty bitmap}

test hwloc-bitmap-3.6.13 "unary bitmap operations, last (infinite set)" -body {
    hwloc bitmap last 0-
} -returnCodes error -result {last is undefined for infinite bitmap}

test hwloc-bitmap-3.6.14 "unary bitmap operations, last (infinite set)" -body {
    hwloc bitmap last 4-
} -returnCodes error -result {last is undefined for infinite bitmap}

foreach {n v r} {
    4  0     0
    5  1     1
    6  0-1   1
    7  0,2   2
    8  0-2   2
    9  0-1,3 3
    10 0,2-3 3
    11 0,4   4
    12 0-1,4 4
} {
    test hwloc-bitmap-3.6.$n "unary bitmap operations, last \"$v\"" -body {
	hwloc bitmap last $v
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# not (bit inversion, result of finite sets is infinite, and vice versa)

foreach {n v r} {
    3  {}    0-
    4  0     1-
    5  1     0,2-
    6  0-1   2-
    7  0,2   1,3-
    8  0-2   3-
    9  0-1,3 2,4-
    10 0,2-3 1,4-
    11 0,4   1-3,5-
    12 0-1,4 2-3,5-
    13 0-    {}
    14 4-    0-3
} {
    test hwloc-bitmap-3.7.$n.0 "unary bitmap operations, not \"$v\"" -body {
	hwloc bitmap not $v
    } -result $r

    # complementary
    test hwloc-bitmap-3.7.$n.1 "unary bitmap operations, not \"$r\"" -body {
	hwloc bitmap not $r
    } -result $v
}

# - -- --- ----- -------- ------------- ---------------------
# weight (number of bits in set, errors on infinite sets)

test hwloc-bitmap-3.10.13 "unary bitmap operations, weight (infinite set)" -body {
    hwloc bitmap weight 0-
} -returnCodes error -result {hwloc_bitmap_weight error}

test hwloc-bitmap-3.10.14 "unary bitmap operations, weight (infinite set)" -body {
    hwloc bitmap weight 4-
} -returnCodes error -result {hwloc_bitmap_weight error}

foreach {n v r} {
    3  {}    0
    4  0     1
    5  1     1
    6  0-1   2
    7  0,2   2
    8  0-2   3
    9  0-1,3 3
    10 0,2-3 3
    11 0,4   2
    12 0-1,4 3
} {
    test hwloc-bitmap-3.10.$n "unary bitmap operations, weight \"$v\"" -body {
	hwloc bitmap weight $v
    } -result $r
}

# # ## ### ##### ######## ############# #####################
## binary operators.

foreach {n op p} {
    0  clear         {bitmap id}
    1  is_set        {bitmap id}
    2  set           {bitmap id}
    3  next          {bitmap prev}
    4  is_included   {bitmapA bitmapB}
    5  and           {bitmapA bitmapB}
    6  andnot        {bitmapA bitmapB}
    7  compare       {bitmapA bitmapB}
    8  compare_first {bitmapA bitmapB}
    9  intersects    {bitmapA bitmapB}
    10 is_equal      {bitmapA bitmapB}
    11 or            {bitmapA bitmapB}
    12 xor           {bitmapA bitmapB}
} {
    test hwloc-bitmap-4.$n.0 "binary bitmap operations, $op, wrong args" -body {
	hwloc bitmap $op
    } -returnCodes error \
	-result "wrong # args: should be \"hwloc bitmap $op $p\""

    test hwloc-bitmap-4.$n.1 "binary bitmap operations, $op, wrong args" -body {
	hwloc bitmap $op X
    } -returnCodes error \
	-result "wrong # args: should be \"hwloc bitmap $op $p\""

    test hwloc-bitmap-4.$n.2 "binary bitmap operations, $op, wrong args" -body {
	hwloc bitmap $op X Y Z
    } -returnCodes error \
	-result "wrong # args: should be \"hwloc bitmap $op $p\""

    test hwloc-bitmap-4.$n.3 "binary bitmap operations, $op, bad bitmap" -body {
	hwloc bitmap $op X Y
    } -returnCodes error \
	-result {Expected bitmap but got "X"}

    if {$n >= 4} {
	test hwloc-bitmap-4.$n.4 "binary bitmap operations, $op, bad bitmap" -body {
	    hwloc bitmap $op 0- Y
	} -returnCodes error \
	    -result {Expected bitmap but got "Y"}

    } else {
	test hwloc-bitmap-4.$n.4 "binary bitmap operations, $op, bad integer" -body {
	    hwloc bitmap $op 0- Y
	} -returnCodes error \
	    -result {expected integer but got "Y"}
    }
}

# - -- --- ----- -------- ------------- ---------------------
# clear = 'andnot' for single bit.

foreach {n v id r} {
    5  0-    0 1-
    6  3-    0 3-
    7  0-    3 0-2,4-
    8  1,4,7 0 1,4,7
    9  1,4,7 4 1,7
    10 {}    3 {}
} {
    test hwloc-bitmap-4.0.$n "binary bitmap operations, clear, \"$v - $id\"" -body {
	hwloc bitmap clear $v $id
    } -result $r
}

test hwloc-bitmap-4.0.11 "binary bitmap operations, clear, \"{} - -1\"" -body {
    hwloc bitmap clear {} -1
} -returnCodes error -result {Expected integer >= 0 but got "-1"}

# - -- --- ----- -------- ------------- ---------------------
# is_set = test of single bit.

foreach {n v id r} {
    5  0-    0 1
    6  3-    0 0
    7  0-    3 1
    8  1,4,7 0 0
    9  1,4,7 4 1
    10 {}    3 0
} {
    test hwloc-bitmap-4.1.$n "binary bitmap operations, is_set, \"$v ? $id\"" -body {
	hwloc bitmap is_set $v $id
    } -result $r
}

test hwloc-bitmap-4.1.11 "binary bitmap operations, is_set, \"{} ? -1\"" -body {
    hwloc bitmap is_set {} -1
} -returnCodes error -result {Expected integer >= 0 but got "-1"}

# - -- --- ----- -------- ------------- ---------------------
# set = 'or' for single bit.

foreach {n v id r} {
    5  0-    0 0-
    6  3-    0 0,3-
    7  0-    3 0-
    8  1,4,7 0 0-1,4,7
    9  1,4,7 4 1,4,7
    10 {}    3 3
} {
    test hwloc-bitmap-4.2.$n "binary bitmap operations, set, \"$v + $id\"" -body {
	hwloc bitmap set $v $id
    } -result $r
}

test hwloc-bitmap-4.2.11 "binary bitmap operations, set, \"{} + -1\"" -body {
    hwloc bitmap set {} -1
} -returnCodes error -result {Expected integer >= 0 but got "-1"}

# - -- --- ----- -------- ------------- ---------------------
# next = find next set bit greater than 'prev'. for iteration
# over a set, adjunct to the unary method 'first'.

foreach {n v id r} {
    5  0-     0 1
    6  3-     0 3
    7  0-     3 4
    8  1,4,7  0 1
    9  1,4,7  4 7
    10 3,2   -1 2
} {
    test hwloc-bitmap-4.3.$n "binary bitmap operations, next, \"$v $id\"" -body {
	hwloc bitmap next $v $id
    } -result $r
}

test hwloc-bitmap-4.3.11 "binary bitmap operations, next, \"empty\", fails" -body {
    hwloc bitmap next {} 3
} -returnCodes error -result {next is undefined for empty bitmap}

test hwloc-bitmap-4.3.12 "binary bitmap operations, next, \"empty after prev\", fails" -body {
    hwloc bitmap next 1 3
} -returnCodes error -result {next is undefined for a bitmap empty after bit "3"}

# - -- --- ----- -------- ------------- ---------------------
# is_included = is A subset of B?

foreach {n va vb r} {
    5  0-    0-     1
    6  0-    1-     0
    7  1,4,7 0-     1
    8  1,4,7 9-     0
    9  0-    1,4,7  0
    10 9-    1,4,7  0
    11 1,4,7 1,4    0
    12 1,4   1,4,7  1
    13 1,4,7 1,4,7  1
    14 {}    1,4,7  1
    15 1,4,7 {}     0
    16 {}    6-     1
    17 6-    {}     0
} {
    test hwloc-bitmap-4.4.$n "binary bitmap operations, is_included, \"$va subset $vb\"" -body {
	hwloc bitmap is_included $va $vb
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# and = A bit-and B (A intersection B)

foreach {n va vb r} {
    5  0-    0-     0-
    6  0-    1-     1-
    7  1,4,7 0-     1,4,7
    8  1,4,7 9-     {}
    9  0-    1,4,7  1,4,7
    10 9-    1,4,7  {}
    11 1,4,7 1,4    1,4
    12 1,4   1,4,7  1,4
    13 1,4,7 1,4,7  1,4,7
    14 {}    1,4,7  {}
    15 1,4,7 {}     {}
    16 {}    6-     {}
    17 6-    {}     {}
} {
    test hwloc-bitmap-4.5.$n "binary bitmap operations, and, \"$va bitand $vb\"" -body {
	hwloc bitmap and $va $vb
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# andnot = A bit-and (bit-not B) (subtraction)

foreach {n va vb r} {
    5  0-    0-     {}
    6  0-    1-     0
    7  1,4,7 0-     {}
    8  1,4,7 9-     1,4,7
    9  0-    1,4,7  0,2-3,5-6,8-
    10 9-    1,4,7  9-
    11 1,4,7 1,4    7
    12 1,4   1,4,7  {}
    13 1,4,7 1,4,7  {}
    14 {}    1,4,7  {}
    15 1,4,7 {}     1,4,7
    16 {}    6-     {}
    17 6-    {}     6-
} {
    test hwloc-bitmap-4.6.$n "binary bitmap operations, andnot, \"$va subtract $vb\"" -body {
	hwloc bitmap andnot $va $vb
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# compare = last A == last B ?

foreach {n va vb r} {
    5  0-    0-     0
    6  0-    1-     1
    7  1,4,7 0-     -1
    8  1,4,7 9-     -1
    9  0-    1,4,7  1
    10 9-    1,4,7  1
    11 1,4,7 1,4    1
    12 1,4   1,4,7  -1
    13 1,4,7 1,4,7  0
    14 {}    1,4,7  -1
    15 1,4,7 {}     1
    16 {}    6-     -1
    17 6-    {}     1
} {
    test hwloc-bitmap-4.7.$n "binary bitmap operations, compare, \"$va same-highest? $vb\"" -body {
	hwloc bitmap compare $va $vb
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# compare_first = first A == first B ?

foreach {n va vb r} {
    5  0-    0-     0
    6  0-    1-     -1
    7  1,4,7 0-     1
    8  1,4,7 9-     -8
    9  0-    1,4,7  -1
    10 9-    1,4,7  8
    11 1,4,7 1,4    0
    12 1,4   1,4,7  0
    13 1,4,7 1,4,7  0
    14 {}    1,4,7  2
    15 1,4,7 {}     -2
    16 {}    6-     7
    17 6-    {}     -7
} {
    test hwloc-bitmap-4.8.$n "binary bitmap operations, compare_first, \"$va same-first? $vb\"" -body {
	hwloc bitmap compare_first $va $vb
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# intersect = non-empty? (A bit-and B) == (non-empty? (A intersection B))

foreach {n va vb r} {
    5  0-    0-     1
    6  0-    1-     1
    7  1,4,7 0-     1
    8  1,4,7 9-     0
    9  0-    1,4,7  1
    10 9-    1,4,7  0
    11 1,4,7 1,4    1
    12 1,4   1,4,7  1
    13 1,4,7 1,4,7  1
    14 {}    1,4,7  0
    15 1,4,7 {}     0
    16 {}    6-     0
    17 6-    {}     0
} {
    test hwloc-bitmap-4.9.$n "binary bitmap operations, intersects, \"$va non-empty-intersection $vb\"" -body {
	hwloc bitmap intersects $va $vb
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# is_equal = A set-equal B ?

foreach {n va vb r} {
    5  0-    0-     1
    6  0-    1-     0
    7  1,4,7 0-     0
    8  1,4,7 9-     0
    9  0-    1,4,7  0
    10 9-    1,4,7  0
    11 1,4,7 1,4    0
    12 1,4   1,4,7  0
    13 1,4,7 1,4,7  1
    14 {}    1,4,7  0
    15 1,4,7 {}     0
    16 {}    6-     0
    17 6-    {}     0
} {
    test hwloc-bitmap-4.10.$n "binary bitmap operations, is_equal, \"$va set-equal $vb\"" -body {
	hwloc bitmap is_equal $va $vb
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# or = A bit-or B (A union B)

foreach {n va vb r} {
    5  0-    0-     0-
    6  0-    1-     0-
    7  1,4,7 0-     0-
    8  1,4,7 9-     1,4,7,9-
    9  0-    1,4,7  0-
    10 9-    1,4,7  1,4,7,9-
    11 1,4,7 1,4    1,4,7
    12 1,4   1,4,7  1,4,7
    13 1,4,7 1,4,7  1,4,7
    14 {}    1,4,7  1,4,7
    15 1,4,7 {}     1,4,7
    16 {}    6-     6-
    17 6-    {}     6-
} {
    test hwloc-bitmap-4.11.$n "binary bitmap operations, or, \"$va union $vb\"" -body {
	hwloc bitmap or $va $vb
    } -result $r
}

# - -- --- ----- -------- ------------- ---------------------
# xor = A bit-exclusive or B

foreach {n va vb r} {
    5  0-    0-     {}
    6  0-    1-     0
    7  1,4,7 0-     0,2-3,5-6,8-
    8  1,4,7 9-     1,4,7,9-
    9  0-    1,4,7  0,2-3,5-6,8-
    10 9-    1,4,7  1,4,7,9-
    11 1,4,7 1,4    7
    12 1,4   1,4,7  7
    13 1,4,7 1,4,7  {}
    14 {}    1,4,7  1,4,7
    15 1,4,7 {}     1,4,7
    16 {}    6-     6-
    17 6-    {}     6-
} {
    test hwloc-bitmap-4.12.$n "binary bitmap operations, xor, \"$va xor $vb\"" -body {
	hwloc bitmap xor $va $vb
    } -result $r
}

# # ## ### ##### ######## ############# #####################
## trinary operators

# set_range     BITMAP START_ID END_ID
# clear_range   BITMAP START_ID END_ID

foreach {n op} {
    0 set_range
    1 clear_range
} {
    test hwloc-bitmap-5.$n.0 "trinary bitmap operations, $op, wrong args" -body {
	hwloc bitmap $op
    } -returnCodes error \
	-result "wrong # args: should be \"hwloc bitmap $op bitmap begin end\""

    test hwloc-bitmap-5.$n.1 "trinary bitmap operations, $op, wrong args" -body {
	hwloc bitmap $op X
    } -returnCodes error \
	-result "wrong # args: should be \"hwloc bitmap $op bitmap begin end\""

    test hwloc-bitmap-5.$n.2 "trinary bitmap operations, $op, wrong args" -body {
	hwloc bitmap $op X Y
    } -returnCodes error \
	-result "wrong # args: should be \"hwloc bitmap $op bitmap begin end\""

    test hwloc-bitmap-5.$n.3 "trinary bitmap operations, $op, wrong args" -body {
	hwloc bitmap $op X Y Z W
    } -returnCodes error \
	-result "wrong # args: should be \"hwloc bitmap $op bitmap begin end\""

    test hwloc-bitmap-5.$n.4 "trinary bitmap operations, $op, bad bitmap" -body {
	hwloc bitmap $op X Y Z
    } -returnCodes error \
	-result {Expected bitmap but got "X"}

    test hwloc-bitmap-5.$n.5 "trinary bitmap operations, $op, bad start index" -body {
	hwloc bitmap $op 0- Y Z
    } -returnCodes error \
	-result {expected integer but got "Y"}

    test hwloc-bitmap-5.$n.6 "trinary bitmap operations, $op, bad end index" -body {
	hwloc bitmap $op 0- 5 Z
    } -returnCodes error \
	-result {expected integer but got "Z"}
}

# - -- --- ----- -------- ------------- ---------------------
# set-range = bitmap + range of bits.

foreach {n v s e r} {
    7  {}    0 2 0-2
    8  {}    4 8 4-8
    9  1,4,7 0 2 0-2,4,7
    10 1,4,7 4 8 1,4-8
    11 0-    0 2 0-
    12 0-    4 8 0-
    13 9-    0 2 0-2,9-
    14 9-    4 8 4-
    15 {}    8 4 {}
    16 9-    8 4 9-
} {
    test hwloc-bitmap-5.0.$n "trinary bitmap operations, setrange, \"$v + $s..$e\"" -body {
	hwloc bitmap set_range $v $s $e
    } -result $r
}

test hwloc-bitmap-5.0.17 "trinary bitmap operations, set_range, negative bits" -body {
    hwloc bitmap set_range {} -1 5
} -returnCodes error -result {Expected integer >= 0 but got "-1"}

test hwloc-bitmap-5.0.18 "trinary bitmap operations, set_range, negative bits" -body {
    hwloc bitmap set_range {} 5 -1
} -returnCodes error -result {Expected integer >= 0 but got "-1"}

# - -- --- ----- -------- ------------- ---------------------
# clear_range = bitmap - range of bits.

foreach {n v s e r} {
    7  {}    0 2 {}
    8  {}    4 8 {}
    9  1,4,7 0 2 4,7
    10 1,4,7 4 8 1
    11 0-    0 2 3-
    12 0-    4 8 0-3,9-
    13 9-    0 2 9-
    14 9-    4 8 9-
    15 {}    8 4 {}
    16 0-    8 4 0-

} {
    test hwloc-bitmap-5.1.$n "trinary bitmap operations, clear_range, \"$v - $e..$e\"" -body {
	hwloc bitmap clear_range $v $s $e
    } -result $r
}

test hwloc-bitmap-5.1.17 "trinary bitmap operations, clear_range, negative bits" -body {
    hwloc bitmap clear_range {} -1 5
} -returnCodes error -result {Expected integer >= 0 but got "-1"}

test hwloc-bitmap-5.1.18 "trinary bitmap operations, clear_range, negative bits" -body {
    hwloc bitmap clear_range {} 5 -1
} -returnCodes error -result {Expected integer >= 0 but got "-1"}

# # ## ### ##### ######## ############# #####################
tcltest::cleanupTests
